var express = require('express');
var router = express.Router();
const axios = require('axios')

//定义路由，当访问该路由的时候，去数据库查找信息并返回
router.get('/', function (req, res, next) {
    let _res = res
    let code = req.query.code
    axios.get(`https://api.weixin.qq.com/sns/jscode2session?appid=wx46d4c5148b511382&secret=5ee2348497f892ed1eee310fdc78f784&js_code=${code}&grant_type=authorization_code`).then(res => {
        if (res.data.openid) {
            let data = res.data
            _res.json({ openid: data.openid, code: 200, msg: '返回成功！' })
        }
        else {
            _res.json({ errmsg: res.data.errmsg, errcode: res.data.errcode })
        }
    })
})

module.exports = router;
